package com.caijin.common.util;

import com.caijin.common.core.domain.Result;
import com.caijin.common.core.domain.entity.Catalogue;
import com.caijin.common.core.domain.entity.Comment;
import com.caijin.common.core.domain.entity.Department;
import com.caijin.common.core.domain.entity.Menu;
import com.caijin.common.core.domain.vo.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 树状结构转换工具
 *
 * @author Peas
 */
public class ListToTree {

    /**
     * 数据库中拿到的菜单数据转换为树状结构：主页面
     *
     * @param data 菜单数据
     * @return
     */
    public static Result menuToTree(List<Menu> data) {
        Map<Long, MenuVO> map = new HashMap<>();
        map.put(0L, new MenuVO());
        List<MenuVO> dataVO = data.stream().map(menu -> menu.toVO()).collect(Collectors.toList());
        dataVO.forEach(menu -> map.put(menu.getId(), menu));
        dataVO.forEach(menu -> {
            MenuVO parent = map.get(menu.getParentId());
            if (parent != null) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(menu);
            }
        });
        List<MenuVO> returndata = map.get(0L).getChildren();
        return Result.ok().data("data", returndata);
    }

    /**
     * 数据库中拿到的菜单数据转换为树状结构：课程页面
     *
     * @param data 菜单数据
     * @return
     */
    public static Result menuToTreeForCourse(List<Menu> data) {
        Map<Long, MenuVO> map = new HashMap<>();
        map.put(999L, new MenuVO());
        List<MenuVO> dataVO = data.stream().map(menu -> menu.toVO()).collect(Collectors.toList());
        dataVO.forEach(menu -> map.put(menu.getId(), menu));
        dataVO.forEach(menu -> {
            MenuVO parent = map.get(menu.getParentId());
            if (parent != null) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(menu);
            }
        });
        List<MenuVO> returndata = map.get(999L).getChildren();
        return Result.ok().data("data", returndata);
    }

    /**
     * 数据库中拿到的部门数据转换为树状结构
     *
     * @param data 部门数据
     * @return 部门目录
     */
    public static Result departmentToTree(List<Department> data) {
        Map<Long, DepartmentVO> map = new HashMap<>();
        map.put(0L, new DepartmentVO());
        List<DepartmentVO> dataVO = data.stream().map(department -> department.toVO()).collect(Collectors.toList());
        dataVO.forEach(department -> map.put(department.getValue(), department));
        dataVO.forEach(departmentVO -> {
            DepartmentVO parent = map.get(departmentVO.getParentId());
            if (parent != null) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(departmentVO);
            }
        });
        List<DepartmentVO> returnData = map.get(0L).getChildren();
        return Result.ok().data("data", returnData);
    }

    /**
     * 数据库中拿到的章节目录数据转换为树状结构（不包含内容）
     *
     * @param data 章节数据
     * @return 章节目录
     */
    public static Result catalogueToTreeForList(List<CatalogueTagVO> data) {
        return Result.ok().data("data", catalogueTagToFree(data, 0L));
    }

    public static List<CatalogueTagVO> catalogueTagToFree(List<CatalogueTagVO> data, Long id) {
        Map<Long, CatalogueTagVO> map = new HashMap<>();
        CatalogueTagVO catalogueVO1 = new CatalogueTagVO();
        catalogueVO1.setLabel("章节目录");
        catalogueVO1.setId(id);
        map.put(id, catalogueVO1);
        data.stream().forEach(catalogueTagVO -> map.put(catalogueTagVO.getId(), catalogueTagVO));
        data.forEach(CatalogueTagVO -> {
            CatalogueTagVO parent = map.get(CatalogueTagVO.getParentId());
            if (parent != null) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(CatalogueTagVO);
            }
        });
        List<CatalogueTagVO> returnData = new ArrayList<>();
        returnData.add(map.get(id));
        return returnData;
    }

    public static List<CatalogueVO> catalogueToFree(List<Catalogue> data, Long id) {
        Map<Long, CatalogueVO> map = new HashMap<>();
        CatalogueVO catalogueVO1 = new CatalogueVO();
        catalogueVO1.setLabel("章节目录");
        catalogueVO1.setId(id);
        map.put(id, catalogueVO1);
        List<CatalogueVO> collect = data.stream().map(catalogueVO -> {
            CatalogueVO catalogueVO2 = catalogueVO.toVO();
            map.put(catalogueVO2.getId(), catalogueVO2);
            return catalogueVO2;
        }).collect(Collectors.toList());
        collect.forEach(catalogueVO -> {
            CatalogueVO parent = map.get(catalogueVO.getParentId());
            if (parent != null) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(catalogueVO);
            }
        });
        List<CatalogueVO> returnData = new ArrayList<>();
        returnData.add(map.get(id));
        return returnData;
    }

    /**
     * 评论列表转为树结构
     *
     * @param comments
     * @return
     */
    public static List<CommentVO> commentListToTree(List<Comment> comments) {
        Map<Long, CommentVO> map = new HashMap<>();
        CommentVO comment1 = new CommentVO();
        map.put(null, comment1);
        List<CommentVO> collect = comments.stream().map(comment -> {
            CommentVO commentVO = comment.toVO();
            map.put(comment.getId(), commentVO);
            return commentVO;
        }).collect(Collectors.toList());
        collect.stream().forEach(commentVO -> {
            if (map.containsKey(commentVO.getParentId())) {
                CommentVO parentCommentVO = map.get(commentVO.getParentId());
                if (parentCommentVO.getChildren() == null) {
                    parentCommentVO.setChildren(new ArrayList<>());
                }

                if (parentCommentVO.getParentId() == null) {
                    parentCommentVO.getChildren().add(commentVO);
                } else {
                    CommentVO commentVO2 = map.get(parentCommentVO.getParentId());
                    CommentVO commentVO1 = map.get(commentVO.getId());
                    commentVO1.setParentName(parentCommentVO.getNickname());
                    commentVO2.getChildren().add(commentVO1);
                }
            }
        });
        return map.get(null).getChildren();
    }
}
